package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.wetsion.employ.server.mapper.CompanyMapper;
import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.CompanyDetail;
import site.wetsion.employ.server.pojo.CompanyProduct;
import site.wetsion.employ.server.pojo.Domain;
import site.wetsion.employ.server.util.Page;

@Service
public class CompanyService {
	
	private final static Logger log = LoggerFactory.getLogger(CompanyService.class);

	@Autowired
	private CompanyMapper companyMapper;
	/**
	 * 显示所有 的领域行业分类，异常则返回空
	 * @return
	 */
	public List<Domain> showAllDomain(){
		try {
			return companyMapper.getAllDomain();
		} catch (Exception e) {
			log.error("查询所有行业领域出错", e);
			return null;
		}
	}
	/** 根据id获取行业名称*/
	public String getDomainNameById(int id){
		try {
			return companyMapper.getDomainNameById(id);
		} catch (Exception e) {
			log.error("根据id获取行业名称失败", e);
			return null;
		}
	}
	/**
	 * 添加新公司
	 * @param company 公司基本信息
	 * @param product 公司产品信息
	 * @param detail 公司详情信息
	 * @return
	 */
	public boolean addNewCompany(Company company, CompanyProduct product,CompanyDetail detail){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		try {
			detail.setGmt_create(timestamp);
			detail.setGmt_modified(timestamp);
			companyMapper.insertCompanyDetail(detail);
		} catch (Exception e2) {
			log.error("插入公司详情信息失败", e2);
			return false;
		}
		try {
			company.setCompany_detail(detail.getId());
			company.setGmt_create(timestamp);
			company.setGmt_modified(timestamp);
			companyMapper.insertCompany(company);
		} catch (Exception e) {
			log.error("插入公司信息失败", e);
			try {
				//插入公司失败则把前面插入的公司详情删除
				companyMapper.deleteCompanyDetailById(detail.getId());
				return false;
			} catch (Exception e1) {
				log.error("删除公司详情失败", e1);
				return false;
			}
		}
		try {
			product.setProduct_company(company.getId());
			product.setGmt_create(timestamp);
			product.setGmt_modified(timestamp);
			companyMapper.insertProduct(product);
		} catch (Exception e) {
			log.error("插入公司产品信息失败", e);
			try {
				//插入产品失败则把之前插入的公司和公司详情都删除
				companyMapper.deleteCompanyById(company.getId());
				companyMapper.deleteCompanyDetailById(detail.getId());
				return false;
			} catch (Exception e1) {
				log.error("删除公司和详情信息失败", e);
				return false;
			}
		}
		return true;
	}
	
	
	
	/**
	 * 查询公司数
	 * @return
	 */
	public int getCompanyCount(){
		try {
			int num = companyMapper.getCompanyCount();
			return num;
		} catch (Exception e) {
			log.error("查询tb_company总记录数失败", e);
			return 0;
		}
	}
	/**
	 * 分页查询
	 * @param page 页数
	 * @param pageSize 每页显示数量
	 * @return
	 */
	public List<Company> showAllCompanyByPage(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Company> companys = companyMapper.getCompanysByPage((pageNum-1)*pageSize, pageSize);
			for (Company company : companys) {
				String location_name = companyMapper.getLocationNameById(company.getCompany_location());
				System.out.println("a");
				company.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(company.getCompany_domain());
				company.setDomain_name(domain_name);
			}
			return companys;
		} catch (Exception e) {
			log.error("分页查询公司出错", e);
			return null;
		}
	}
	/**
	 * 根据公司名称查询公司，并分页
	 * @param page
	 * @param name 查询的关键字
	 * @return
	 */
	public List<Company> searchCompanyByName(Page page, String name){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		System.out.println("name="+name);
		try {
			List<Company> companys = companyMapper.getCompanyOnSearchNameByPage((pageNum-1)*pageSize, pageSize, name);
			for (Company company : companys) {
				String location_name = companyMapper.getLocationNameById(company.getCompany_location());
				company.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(company.getCompany_domain());
				company.setDomain_name(domain_name);
			}
			return companys;
		} catch (Exception e) {
			log.error("分页根据名称查询公司出错，查询关键字是"+name, e);
			return null;
		}
	}
	/**
	 * 获取符合关键字的记录数量
	 * @param name 关键字
	 * @return
	 */
	public int getSearchCountByName(String name){
		try {
			int count = companyMapper.getSearchCountByName(name);
			return count;
		} catch (Exception e) {
			log.error("安关键字"+name+"查询出错", e);
			return 0;
		}
	}
	
	/**
	 * 根据id删除公司
	 * @param id
	 * @return
	 */
	public boolean deleteCompany(BigInteger id){
		try {
			companyMapper.deleteCompanyById(id);
			return true;
		} catch (Exception e) {
			log.error("删除失败", e);
			return false;
		}
	}
	/**
	 * 清空每日推荐
	 */
	public void deleteRecommedCompany(){
		try {
			companyMapper.deleteHotCompany();
		} catch (Exception e) {
			log.error("清空tb_company_recommed失败", e);
		}
	}
	/**
	 * 添加每日推荐公司
	 * @param id 公司id
	 * @return
	 */
	public boolean addRecommedCompany(BigInteger id){
		try {
			companyMapper.insertRecommedCompany(id);
			return true;
		} catch (Exception e) {
			log.error("插入tb_company_recommed失败", e);
			return false;
		}
	}
	/** 获取每日推荐公司*/
	public BigInteger getRecommedCompany(){
		try {
			return companyMapper.getRecommedCompany();
		} catch (Exception e) {
			log.error("获取每日推荐失败", e);
			return new BigInteger("0");
		}
	}
	/** 根据id查询公司*/
	public Company getCompanyById(BigInteger id){
		try {
			Company company = companyMapper.getCompanyById(id);
			company.setLocation_name(companyMapper.getLocationNameById(company.getCompany_location()));
			company.setDomain_name(getDomainNameById(company.getCompany_domain()));
			return company;
		} catch (Exception e) {
			log.error("根据id="+id+"查询公司失败", e);
			return null;
		}
	}
	/** 根据id查询公司详情*/
	public CompanyDetail getCompanyDetailById(BigInteger id){
		try {
			return companyMapper.getCompanyDetailById(id);
		} catch (Exception e) {
			log.error("根据id="+id+"查询公司详情失败", e);
			return null;
		}
	}
	/** 根据公司id查询产品*/
	public CompanyProduct getCompanyProductByCompanyId(BigInteger id){
		try {
			return companyMapper.getCompanyProductByCompanyId(id);
		} catch (Exception e) {
			log.error("根据公司id="+id+"查询产品失败", e);
			return null;
		}
	}
	
	/** 修改公司信息*/
	public boolean updateCompany(Company company){
		try {
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			company.setGmt_modified(timestamp);
			companyMapper.updateCompany(company);
			return true;
		} catch (Exception e) {
			log.error("修改公司信息失败,公司id="+company.getId(), e);
			return false;
		}
	}
	/** 修改公司产品信息*/
	public boolean updateCompanyProduct(CompanyProduct product){
		try {
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			product.setGmt_modified(timestamp);
			companyMapper.updateCompanyProduct(product);
			return true;
		} catch (Exception e) {
			log.error("修改公司产品信息失败,产品id="+product.getId(), e);
			return false;
		}
	}
	/** 修改公司详情信息*/
	public boolean updateCompanyDetail(CompanyDetail detail){
		try {
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			detail.setGmt_modified(timestamp);
			companyMapper.updateCompanyDetail(detail);
			return true;
		} catch (Exception e) {
			log.error("修改公司详情信息失败,详情id="+detail.getId(), e);
			return false;
		}
	}
	
	public List<Company> searchCompanyByDomain(Page page, int domain){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Company> companys = companyMapper.getCompanyOnDomainByPage((pageNum-1)*pageSize, pageSize, domain);
			for (Company company : companys) {
				String location_name = companyMapper.getLocationNameById(company.getCompany_location());
				company.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(company.getCompany_domain());
				company.setDomain_name(domain_name);
			}
			return companys;
		} catch (Exception e) {
			log.error("分页根据行业查询公司出错，查询行业是"+domain, e);
			return null;
		}
	}
	/**
	 * 获取对应行业的公司数量
	 * @param domain
	 * @return
	 */
	public int getCompanyCountByDomain(int domain){
		try {
			int count = companyMapper.getCompanyCountByDomain(domain);
			return count;
		} catch (Exception e) {
			log.error("安行业"+domain+"查询出错", e);
			return 0;
		}
	}
	/** 获取最新发布的公司*/
	public List<Company> getLastCreateCompany(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			return companyMapper.getLastCreatCompany((pageNum-1)*pageSize, pageSize);
		} catch (Exception e) {
			log.error("获取最新发布的公司失败", e);
			return null;
		}
	}
}
